<?php
// 应用公共文件
/**
 * 驼峰命名转下划线命名
 * @param $str
 * @return string
 */
function toUnderScore($str)
{
    $dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
        return '_' . strtolower($matchs[0]);
    }, $str);
    return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
}
/**
 * API返回json数据
 */
function returnJson($code=200, $msg='success', $data=[])
{
    echo json_encode(["code"=>$code,'msg'=>$msg,'data'=>$data]);die;
}
/**
 *把url中的反斜杠转化为正斜杠
 */
function getForwardSlash($url)
{
    return str_replace("\\","/",$url);
}
/**
 *根据token获取id
 */
function getTokenId($token)
{
    return strrev(substr(strrev($token),1,strpos(strrev($token),"_")-3));
}
/**
 * type str 为GET或者POST
 * post传参调用为:  curlHttp($url,'POST',json_encode(input()))
 * get传参调用为:   curlHttp($url,'GET');
 *
 */
function curlHttp( $url,$type='GET', $data='')
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); //// https请求 不验证证书和hosts
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    if( $type == 'GET')
    {
        curl_setopt($curl, CURLOPT_HEADER, FALSE);  //不返回报文头部
    }else
    {
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json;charset=utf-8'));
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    $output = curl_exec($curl);
    curl_close($curl);
    return json_decode($output,TRUE);
}
